﻿using HandyControl.Controls;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using RecordBill.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RecordBill.ViewModels
{
    public class SettViewModel : BindableBase, INavigationAware
    {
        private UISettInfo _settInfo;

        public UISettInfo SettInfo
        {
            get { return _settInfo; }
            set { _settInfo = value; RaisePropertyChanged(); }
        }
        public DelegateCommand SaveSettCommand
        {
            get;
            set;
            
        }
        public DelegateCommand ChooseFilePathCommand { get; set; }
        private void SaveSett()
        {
            ConfigSettings.Email = SettInfo.Email;
            ConfigSettings.EmailKey = SettInfo.EmailKey;
            ConfigSettings.DownloadDir= SettInfo.FilePath;
        }

        public SettViewModel()
        {
            SettInfo = new UISettInfo();
            SaveSettCommand = new DelegateCommand(SaveSett);
            ChooseFilePathCommand = new DelegateCommand(ChoiceFilePath);
        }

        private void ChoiceFilePath()
        {
              var  dialog= new  System.Windows.Forms.FolderBrowserDialog();
            if (dialog.ShowDialog()==System.Windows.Forms.DialogResult.OK)
            {
                SettInfo.FilePath = dialog.SelectedPath;
            }
            dialog.Dispose();

        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            return;
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
           if(!string.IsNullOrWhiteSpace(ConfigSettings.Email))
            {
                SettInfo.Email=ConfigSettings.Email;
            }
            if (!string.IsNullOrWhiteSpace(ConfigSettings.EmailKey))
            {
                SettInfo.EmailKey = ConfigSettings.EmailKey;
            }
            if (!string.IsNullOrWhiteSpace(ConfigSettings.DownloadDir))
            {
                SettInfo.FilePath = ConfigSettings.DownloadDir;
            }
        }
    }
}
